<?php
namespace app\admin\controller;
use think\facade\{View,Session,Db};
use app\BaseController;

class Basic extends Base
{
    
    public function bdyx(){
        
              if($this->Ajax){
            $page=input("page",1);
            $size = input("limit", 10);



            $lst=Db::name("bdlst")->order("id desc")->page($page,$size)->select()->toArray();
            foreach($lst as &$v){
                $v['addtime']=date("Y-m-d H:i:s",$v['addtime']);
            }
            $count= Db::name("bdlst")->count();

           

            return resmsg(0,"检索成功",$lst, $count);
        }
        return View::fetch();
    }

    public function wxconfig()
    {
        //初始化页面

        $msg=Db::name("wx_basic")->find(1);

        View::assign('msg',$msg);

        return View::fetch('basic/wxconfig');
    }
    // 提交微信配置
    public function setWxConfigSave(){
        $param=input("param.");
        $param['edittime']=time();
       

        if(!empty($_FILES['keyfile']['size'])){
           
            $file = request()->file('keyfile');
            $param['keyfile'] = uniqid() .".".$file->extension();
            \think\facade\Filesystem::disk('efile')->putFileAs("wxconfig", $file,$param['keyfile']);
          }
          if(!empty($_FILES['keyfile1']['size'])){
           
            $file = request()->file('keyfile1');
            $param['keyfile1'] = uniqid() .".".$file->extension();
            \think\facade\Filesystem::disk('efile')->putFileAs("wxconfig", $file,$param['keyfile1']);
          }

 
        if(!empty($_FILES['indeximg']['size'])){
          
            $file = request()->file('indeximg');
            $param['indeximg'] = "indeximg1.jpg";//.$file->extension();
            \think\facade\Filesystem::disk('files')->putFileAs("iuser", $file,$param['indeximg']);
          }
          if(!empty($_FILES['indeximg1']['size'])){
          
            $file = request()->file('indeximg1');
            $param['indeximg1'] = "indeximg2.jpg";
            \think\facade\Filesystem::disk('files')->putFileAs("iuser", $file,$param['indeximg1']);
          }

        Db::name("wx_basic")->where("id",1)->update($param);
        return returnmsg(200,"配置更新成功");
    }
    public function userconfig(){

        $user=Db::name("user_config")->find(input('id'));
        $msg=json_decode($user['content'],true);

        View::assign('msg',$msg);

        View::assign('msgtitle',$user['remark']);

        view::assign('_sht12',_sht12);

        return View::fetch();
    }
  
    public function setUserconfig(){

        $data=[];
        $data['content']=json_encode(input('param.'));
        $data['edittime']=time();

        $is=Db::name("user_config")->where("id",input('id'))->update($data);

        return returnmsg(200,"配置更新成功");

     
    }
    public function doBaiduj(){
       // "1en2AzbKm5Uw5EMeLnO2Hfgs5jKAxVFg@GSdeHgtnP3hmpk9W3NjKN4HDfBRd1v33"
        $token =trim(input('token'));
        // 编辑一条转化数据
        //http://zx.lihongnet.cn/h5/?bd_vid=uANBIyIxUhNLgvw-I-tkrjR4PHR1g1PxnHTsPjT4P101PH6Ynf#/'
        
               try {
              
                        $cv = array(
            'logidUrl' => trim(input('url')), // 您的落地页url
            'convertType' => 3 // 转化类型请按实际情况填写
        );
        // 此处仅为demo, conversionTypes支持添加更多数据
        $conversionTypes = array($cv);
        $Baiduj=new \app\api\service\Baiduj();
        $res=$Baiduj->sendConvertData($token, $conversionTypes);
        
        //--------
             
              
                        $cv = array(
            'logidUrl' => trim(input('url')), // 您的落地页url
            'convertType' => 90 // 转化类型请按实际情况填写
        );
        // 此处仅为demo, conversionTypes支持添加更多数据
        $conversionTypes = array($cv);
        $Baiduj=new \app\api\service\Baiduj();
        $res=$Baiduj->sendConvertData($token, $conversionTypes);
        
        //--------
             $cv = array(
            'logidUrl' => trim(input('url')), // 您的落地页url
            'newType' => 10 // 转化类型请按实际情况填写
        );
        // 此处仅为demo, conversionTypes支持添加更多数据
        $conversionTypes = array($cv);
        $Baiduj=new \app\api\service\Baiduj();
        $res=$Baiduj->sendConvertData($token, $conversionTypes);
                            
                            } catch (\Exception $e) {
                            
                            }
      
        
        
    
        if($res==1){
            if(!Db::name('bdlst')->where("url",trim(input('url')))->find()){
               Db::name('bdlst')->insert([
                'token'=>$token,
                'url'=>trim(input('url')),
                'addtime'=>time()
                ]); 
            }
            
            return returnmsg(200,"操作成功");
        }else{
            return returnmsg(199,"参数异常".$res);  
        }
        
    }
    // 套餐管理
    public function tclst(){

       
        if($this->Ajax){
            $lst=Db::name("tclst")->order("xh asc")->select()->toArray();
            foreach($lst as &$v){
                $v['edittime']=date("Y-m-d H:i:s",$v['edittime']);
            }
            $count= count($lst);

            return resmsg(0,"检索成功",$lst, $count);
        }

        return View::fetch();

    }
    // 套餐
    public function setTcStatus(){
        Db::name("tclst")->where("id",input('id'))->update([
            'status'=>input('status')
        ]);
        return returnmsg(200,"操作成功");
    }
    //套餐更新
    public function tcitem(){

        $msg=Db::name("tclst")->find(input('id'));

        View::assign('msg',$msg);

        return View::fetch('basic/src/tcitem');
    }
    //套餐删除
    
    public function tcDelete(){

        $is=Db::name("tclst")->delete(input('id'));

        return returnmsg(200,"操作成功");
    }
    // 套餐更新
    public function tcItemSave(){
        $param = input('param.', []);
   
        if(input('price')<=0){
            return returnmsg(199,"购买金额必须大于0");
        }
        if(input('day_num')<=0){
            return returnmsg(199,"会员天数必须大于0");
        }
  
        $mp = [];
        $mp[] = $param['id'] ? ["id", "<>", $param['id']] : ["id", ">", 0];
        $is = Db::name("tclst")->where($mp)->where("name", $param['name'])->value("id");



        $param['edittime'] = time();
        if (!$param['id']) {
            unset($param['id']);
            $param['xh']=Db::name("tclst")->count();
        }

        $is = Db::name("tclst")->strict(false)->save($param);

        return $is ? returnmsg(200, "操作成功") : returnmsg(201, "操作失败");

    }
    //套餐排序
    public function tcpxlst(){
        $lst=Db::name("tclst")->order("xh asc")->select()->toArray();
        View::assign('lst',$lst);

        return View::fetch('basic/src/tcpxlst');
    }
    // 套餐排序提交
    public function tcxhSave(){
        $idarr=array_filter( explode(",",input('ids')) );
        if($idarr){
            $arr=[];
            foreach ($idarr as $k => $v){
                $arr[]=[
                    'id'=>$v,
                    'xh'=>$k
                    ];
            }
            

            $el=new \app\admin\model\Tclst();
            $res=$el->saveAll($arr);
        }
      
       return returnmsg(200,"数据更新成功");
    }
    //用户购买套餐明细
    public function usertclst(){
        if($this->Ajax){
            $page=input("page",1);
            $size = input("limit", 10);
            $mp=[];
            if(trim(input('umsg'))){
                $eo=[];
                $eo[]=["nickname|reaname|mobile", "like","%".trim(input('umsg'))."%"];
                $mp[]=["iuser_id","in",Db::name("iuser")->where($eo)->column("id")];
            }
            if (trim(input('fmsg'))) {
                $eo=[];
                $eo[]=["nickname|reaname|mobile", "like","%".trim(input('fmsg'))."%"];
                $mp[]=["iuser_id","in",Db::name("iuser")->where($eo)->column("id")];
            }
            $mp[]=["status","=",1];
            $lst=Db::name("tc_order_lst")->where($mp)->order("id desc")->page($page,$size)->select()->toArray();
            foreach($lst as &$v){
                $v['umsg']=Db::name("iuser")->find($v['iuser_id']);
                $v['fmsg']=Db::name("iuser")->find($v['yprice_userid']);
                $v['paytime']=date("Y-m-d H:i:s",$v['paytime']);
            }
         
            $count= Db::name("tc_order_lst")->where($mp)->count();

           

            return resmsg(0,"检索成功",$lst, $count);
        }
        return View::fetch();

    }
    public function usercon(){
        
        $id=input('type');
        $msg=Db::name("user_config")->where("id",$id)->find();
        
        View::assign('msg',$msg);

        

        return View::fetch();

    }
    // 更新用户 隐私协议
    public function setUsercont(){
       
        $id=input('type');
        $msg=Db::name("user_config")->where("id",$id)->update([
            'content'=>input('content')
        ]);
        return returnmsg(200,"操作成功");
    }
    // 富文本上传文件
    public function uploadFile(){
        $file=[];
        if (request()->file()) {
            $file = request()->file('edit');
        }
        if ($file) {
            $fileName = uniqid() .".".$file->extension();
            \think\facade\Filesystem::disk('efile')->putFileAs('iuser/xy', $file, $fileName);
            $param['keyfile'] = $fileName;
        }
         return ['code'=>0,'msg'=>'上传成功','data'=>_doadmin."static/efile/iuser/xy/".$fileName];
        //return returnmsg(200,"上传成功",_doadmin."/static/iuser/xy/".$fileName);

    }
    // 菜单menujson
    public function menujson(){
        $mp=[];
        if(Session('lt_auser')['id']!=1){
          $role_id=Session('lt_auser')['role_id'];
          //Db::name("limit_operate")->column();
          $limits= json_decode(Db::name("role_user")->where("id",$role_id)->value("limits"),true);
          $mp[]=["id","in",$limits];
        }
  
          $lst=Db::name("limit_operate")->field("id,title,icon,type,url href")->where($mp)->where("pid",0)->where("status",1)->order("xh asc")->select()->toArray();
  
          foreach($lst as &$v){
            $v['icon']="layui-icon ".$v['icon'];
            $v['children']=Db::name("limit_operate")->where($mp)->field("id,title,icon,type,url href,openType")->where("pid",$v['id'])->where("status",1)->order("xh asc")->select()->toArray();
            if(!$v['children']){
                unset($v);
            }
          }
          return json($lst);
  
      }
      public function fklst(){
        if($this->Ajax){
            $page=input("page",1);
            $size = input("limit", 10);
            $mp=[];
            if(trim(input('umsg'))){
                $eo=[];
                $eo[]=["nickname|reaname|mobile", "like","%".trim(input('umsg'))."%"];
                $mp[]=["iuser_id","in",Db::name("iuser")->where($eo)->column("id")];
            }
            if (trim(input('sn'))) {
                $mp[]=["sn","like", "%" . trim(input('sn')) . "%"];
            }
            if (input('t')) {
                switch(input('t')){
                    case 1:
                        $mp[]=["status","=",0];
                        break;
                    case 2:
                        $mp[]=["status","=",1];
                        break;
                    case 3:
                        $mp[]=["status","=",-1];
                        break;
                }
            }
            $lst=Db::name("feedback")->where($mp)->order("id desc")->page($page,$size)->select()->toArray();
            foreach($lst as &$v){
                $v['umsg']=Db::name("iuser")->find($v['iuser_id']);
                $v['addtime']=date("Y-m-d H:i:s",$v['addtime']);
                $v['cltime']=date("Y-m-d H:i:s",$v['cltime']);
                if($v['status']){
                    $v['shid_msg']=json_decode($v['ausermsg'],true);
                } 
            }
         
            $count= Db::name("feedback")->where($mp)->count();

           

            return resmsg(0,"检索成功",$lst, $count);
        }

        return View::fetch();
      }
      public function clfeeb(){
        $msg=Db::name("feedback")->find(input('id'));
        $msg['umsg']=Db::name("iuser")->find($msg['iuser_id']);
        View::assign('msg',$msg);
        return View::fetch('basic/src/clfeeb');
      }
        // 审核操作
    public function clItemup(){
        $msg= Db::name("feedback")->where('status', 0)->find(input('id'));
        if(!$msg){
            return returnmsg(199,"该反馈单状态错误");
        }
        $data=[];
        $data['clremark']=trim(input('sh_remark'));
        $data['status']=input('status',0);
        $data['cltime']=time();
        $data['ausermsg'] = json_encode( Db::name("auser")->field('nickname,reaname,mobile,img')->find(Session('lt_auser')['id'] ));
        
        // 启动事务
        Db::startTrans();
        try {
            Db::name("feedback")->where("id",input("id"))->update($data);
           
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return returnmsg(199,"数据异常：".$e->getMessage());
        }
        return returnmsg(200,"操作成功");
    
    }
}
